//
//  EPSlider.swift
//  EPHome
//
//  Created by liujianlong on 2022/9/8.
//

import UIKit
protocol EPSliderDelegate:NSObject{
    func didChange(_ slider:EPSlider)
    
}
class EPSlider: UIView {
    @IBOutlet var minLbl: UILabel!
    @IBOutlet var maxLbl: UILabel!
    @IBOutlet var line: UIView!
    @IBOutlet var leftPoint: UIView!
    @IBOutlet var rightPoint: UIView!
    @IBOutlet var leftPleft: NSLayoutConstraint!
    @IBOutlet var rightPright: NSLayoutConstraint!
    var path = CGMutablePath()
    var min = 0
    var max = 100
    var originMin = 0
    var originMax = 100
    var delegate:EPSliderDelegate?
    override func awakeFromNib() {
        super.awakeFromNib()
        let pan1 = UIPanGestureRecognizer(target:self, action:#selector(leftPan))
        let pan2 = UIPanGestureRecognizer(target:self, action:#selector(rightPan))
        self.leftPoint.addGestureRecognizer(pan1)
        self.rightPoint.addGestureRecognizer(pan2)
        self.min = self.originMin
        self.max = self.originMax
        self.minLbl.text = "\(self.originMin)"
        self.maxLbl.text = "\(self.originMax)"
        self.setLayer(self.leftPoint.layer, "#8193FF".color())
        self.setLayer(self.rightPoint.layer, "#ED593B".color())
    }
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        self.updateShape()
    }
    
    ///设置有效区间的
    func updateShape(){
        self.path = CGMutablePath()
        self.path.move(to:CGPoint(x:self.leftPleft.constant + self.leftPoint.frame.width - self.line.frame.origin.x, y: 0))
        self.path.addLine(to: CGPoint(x:self.frame.width - self.rightPright.constant - self.rightPoint.frame.width - self.line.frame.origin.x, y: 0))
        self.lineShape.path = self.path
    }
    
    lazy var lineShape : CAShapeLayer = {
        let shape = CAShapeLayer()
        let w = self.line.bounds.size.width
        let h = self.line.bounds.size.height
        shape.frame = CGRect(x:0, y:0, width:w, height:h + 2)
        shape.position = CGPoint(x:w / 2.0, y:h+1)
        shape.fillColor = UIColor.clear.cgColor
        shape.strokeColor = "#00E0D3".color().cgColor
        shape.lineWidth = 5
        shape.lineCap = .round
        self.line.layer.addSublayer(shape)
        return shape
    }()
    
    ///设置阴影
    func setLayer(_ layer:CALayer,_ color:UIColor){
        layer.masksToBounds = false
        layer.shadowColor = color.cgColor
        layer.shadowOffset = CGSize(width:0, height:0)
        layer.shadowRadius = 4
        layer.shadowOpacity = 1
        layer.cornerRadius = layer.bounds.width / 2
    }
    
    @objc func leftPan(_ pan:UIPanGestureRecognizer){
        let p = pan.location(in: self)
        if p.y >= 0 && p.y <= self.frame.height{
            self.leftPleft.constant =  p.x
            ///固定锚位，不超过左边point
            if self.leftPleft.constant + self.rightPright.constant + self.leftPoint.frame.width + self.rightPoint.frame.width > self.frame.width{
                self.leftPleft.constant = self.frame.width - self.rightPright.constant - self.rightPoint.frame.width - self.leftPoint.frame.width
            }
            if self.leftPleft.constant <= 0{
                self.leftPleft.constant = 0
            }
            self.updateShape()
            self.update()
        }
    }
    @objc func rightPan(_ pan:UIPanGestureRecognizer){
        let p = pan.location(in: self)
        if p.y >= 0 && p.y <= self.frame.height{
            self.rightPright.constant = self.frame.width - p.x
            ///固定锚位，不超过左边point
            if self.leftPleft.constant + self.rightPright.constant + self.leftPoint.frame.width + self.rightPoint.frame.width > self.frame.width{
                self.rightPright.constant = self.frame.width - self.leftPleft.constant - self.leftPoint.frame.width - self.rightPoint.frame.width
                
            }
            if self.rightPright.constant <= 0{
                self.rightPright.constant = 0
            }
            self.updateShape()
            self.update()
        }
    }
    
    func update()
    {
        let scale = CGFloat(self.originMax - self.originMin) / (self.frame.width - self.leftPoint.frame.width - self.rightPoint.frame.width)
        self.min = Int(self.leftPleft.constant * scale)
        self.max = self.originMax - self.originMin - Int(self.rightPright.constant * scale)
        self.minLbl.text = "\(self.min)"
        self.maxLbl.text = "\(self.max)"
        weak var weakself = self
        self.delegate?.didChange(weakself!)
    }
    
}

